package hlhj.fhp.burst

import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.os.Message
import android.support.v4.view.ViewPager
import android.widget.Button
import android.widget.LinearLayout

/**
 * Created by Administrator on 2018\5\18 0018.
 */

object IndictorUtil{
    //设置指示器
    fun setIndicator(data: List<*>, context: Context, parent: LinearLayout, viewPager: ViewPager) {
        val isAuto = booleanArrayOf(false)
        @SuppressLint("HandlerLeak") val handler = @SuppressLint("HandlerLeak")
        object : Handler() {
            override fun handleMessage(msg: Message) {
                val curPage = viewPager.currentItem
                isAuto[0] = true
                viewPager.currentItem = curPage + 1
            }
        }
        for (j in data.indices) {
            val bt = Button(context)
            bt.setBackgroundResource(R.drawable.radiobt_slct)
            val params = LinearLayout.LayoutParams(context.resources.getDimension(R.dimen.indector).toInt(), context.resources.getDimension(R.dimen.indector).toInt())
            params.setMargins(10, 0, 0, 0)
            bt.layoutParams = params
            parent.addView(bt)
            val childAt = parent.getChildAt(0)
            childAt.setBackgroundResource(R.drawable.radiobt_slct1)
        }
        handler.sendEmptyMessageDelayed(1, 1500)
        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                for (i in 0 until parent.childCount) {
                    val childAt = parent.getChildAt(i)
                    childAt.setBackgroundResource(R.drawable.radiobt_slct)
                }
                parent.getChildAt(position % data.size).setBackgroundResource(R.drawable.radiobt_slct1)
                if (isAuto[0]) {
                    handler.sendEmptyMessageDelayed(1, 3500)
                    isAuto[0] = false
                }
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })

    }
}
